package com.baox.agridata.synchronous.service.impl;

import com.baox.agridata.synchronous.entity.*;
import com.baox.agridata.synchronous.result.Result;
import com.baox.agridata.synchronous.service.*;
import com.baox.agridata.synchronous.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AsyncXDRBaseInfoServiceImpl implements AsyncXDRBaseInfoService {


    @Autowired
    private TXDRCarService txdrCarService;

    @Autowired
    private TXDRSlaughterHouseService txdrSlaughterHouseService;


    @Autowired
    private TXDRHatcheryService txdrHatcheryService;


    @Autowired
    private TXDRTraffickingService txdrTraffickingService;

    @Autowired
    private TXDRFarmService txdrFarmService;


    @Override
    public Result dealReceiveListXDRCarData(List<TXDRCarEntity> l_list) {  //成都发送到四川的【车辆】数据
        try {
            long start = System.currentTimeMillis();
            Integer insertCount = 0, updateCount = 0;
            StringBuilder sb = new StringBuilder();
            sb.append(".java").append(DateUtil.dateConvertTag(new Date()));
            for (TXDRCarEntity data : l_list) {
                TXDRCarEntity txdrCarEntity = txdrCarService.selectTXDRCarById(data.getMid());
                if (txdrCarEntity == null) {
                    data.setSourceId(sb.toString());
                    insertCount += txdrCarService.insertIntoTXDRCar(data);
                } else {
                    updateCount += txdrCarService.updateTXDRCarById(data);
                }
            }
            log.info("method dealReceiveListXDRCarData, insertCount: {},updateCount:{},total:{},总耗时：{}", insertCount, updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【XDRCar】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    public Result dealReceiveListXDRSlaughterHouseData(List<TXDRSlaughterHouseEntity> l_list) {//成都发送到四川的【屠宰场】数据
        try {
            long start = System.currentTimeMillis();
            Integer insertCount = 0, updateCount = 0;
            StringBuilder sb = new StringBuilder();
            sb.append(".java").append(DateUtil.dateConvertTag(new Date()));
            for (TXDRSlaughterHouseEntity data : l_list) {
                TXDRSlaughterHouseEntity txdrCarEntity = txdrSlaughterHouseService.selectTXDRSlaughterHouseById(data.getMid());
                if (txdrCarEntity == null) {
                    data.setSourceId(sb.toString());
                    insertCount += txdrSlaughterHouseService.insertIntoTXDRSlaughterHouse(data);
                } else {
                    updateCount += txdrSlaughterHouseService.updateTXDRSlaughterHouseById(data);
                }
            }
            log.info("method dealReceiveListXDRSlaughterHouseData, insertCount: {},updateCount:{},total:{},总耗时：{}", insertCount, updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【XDRSlaughterHouse】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }


    @Override
    public Result dealReceiveListXDRTraffickingData(List<TXDRTraffickingEntity> l_list) {//成都发送到四川的【贩运户】数据
        try {
            long start = System.currentTimeMillis();
            Integer insertCount = 0, updateCount = 0;
            StringBuilder sb = new StringBuilder();
            sb.append(".java").append(DateUtil.dateConvertTag(new Date()));
            for (TXDRTraffickingEntity data : l_list) {
                TXDRTraffickingEntity txdrTraffickingEntity = txdrTraffickingService.selectTXDRTraffickingById(data.getMid());
                if (txdrTraffickingEntity == null) {
                    data.setSourceId(sb.toString());
                    insertCount += txdrTraffickingService.insertIntoTXDRTrafficking(data);
                } else {
                    updateCount += txdrTraffickingService.updateTXDRTraffickingById(data);
                }
            }
            log.info("method dealReceiveListXDRSlaughterHouseData, insertCount: {},updateCount:{},total:{},总耗时：{}", insertCount, updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【XDRSlaughterHouse】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }


    @Override
    public Result dealReceiveListXDRFarmData(List<TXDRFarmEntity> l_list) {//成都发送到四川的【养殖场户】数据
        try {
            long start = System.currentTimeMillis();
            Integer insertCount = 0, updateCount = 0;
            StringBuilder sb = new StringBuilder();
            sb.append(".java").append(DateUtil.dateConvertTag(new Date()));
            for (TXDRFarmEntity data : l_list) {
                TXDRFarmEntity txdrFarmEntity = txdrFarmService.selectTXDRFarmById(data.getMid());
                if (txdrFarmEntity == null) {
                    data.setSourceId(sb.toString());
                    insertCount += txdrFarmService.insertIntoTXDRFarm(data);
                } else {
                    updateCount += txdrFarmService.updateTXDRCarById(data);
                }
            }
            log.info("method dealReceiveListXDRFarmData, insertCount: {},updateCount:{},total:{},总耗时：{}", insertCount, updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【XDRTrafficking】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }


    //----------------------------------------------------孵化场再说----------------------------------------------------


    @Override
    public Result dealReceiveListXDRHatcheryData(List<TXDRHatcheryEntity> l_list) {//成都发送到四川的【孵化场】数据
        StringBuilder sb = new StringBuilder();
        sb.append(".java").append(DateUtil.dateConvertTag(new Date()));
        for (TXDRHatcheryEntity data : l_list) {
            TXDRHatcheryEntity txdrHatcheryEntity = txdrHatcheryService.selectTXDRHatcheryById(data.getMid());
            if (txdrHatcheryEntity == null) {
                data.setSourceId(sb.toString());
                txdrHatcheryService.insertIntoTXDRHatchery(data);
            }
        }
        return Result.ok();
    }
}
